package com.molyfun.parents.activity.mainpage.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.mainpage.view.TrainRecommendView;
import com.molyfun.parents.rest.bean.CustomPlanlistResponse;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class TrainRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<CustomPlanlistResponse.PlanListBean> mList;

    public TrainRecommendAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<CustomPlanlistResponse.PlanListBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(mList != null){
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<CustomPlanlistResponse.PlanListBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CustomPlanViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_train_recommend_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CustomPlanlistResponse.PlanListBean planListBean = mList.get(position);
        CustomPlanViewHolder customPlanViewHolder = (CustomPlanViewHolder)holder;
        customPlanViewHolder.mTrainRecommendView.initData(planListBean, position != (getItemCount() - 1));
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }



    public class CustomPlanViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.train_recommend_view)
        public TrainRecommendView mTrainRecommendView;

        public CustomPlanViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
